package com.toivan.mt.fragment;

import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.shizhefei.fragment.LazyFragment;
import com.toivan.mt.R;
import com.toivan.mt.adapter.MtPortraitAdapter;
import com.toivan.mt.model.MtPortrait;
import com.toivan.mt.utils.MtCallback;
import com.toivan.mt.utils.MtJsonParser;
import com.toivan.sdk.MtSDK;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MtPortraitFragment
 * @Description 人像抠图的Fragment
 * @Author Heze14
 * @Date 2021/6/22 10:41
 */
public class MtPortraitFragment extends LazyFragment {


    MtPortraitAdapter adapter;

    private List<MtPortrait> list = new ArrayList<>();


    /**
     * 懒加载
     *
     * @param savedInstanceState
     */
    @Override protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_sticker_recyclerview);
        try {
            requireContext();
        } catch (IllegalStateException e) {
            return;
        }
        list.clear();
        List<MtPortrait> portraits = MtJsonParser.getInstance().getAllPortrait();
        if (portraits == null || portraits.size() == 0) {

            MtJsonParser.getInstance().getPortraits( new MtCallback<List<MtPortrait>>() {
                @Override public void callback(List<MtPortrait> bean) {
                    if (bean != null) {
                        list.addAll(bean);
                        showPortrait();
                    }
                }
            });
        } else {
            list.addAll(portraits);
            showPortrait();
        }

    }


    /**
     * 显示
     */
    public void showPortrait() {
        RecyclerView portraitRV = (RecyclerView) findViewById(R.id.recyclerView);
        adapter = new MtPortraitAdapter(list);
        portraitRV.setLayoutManager(new GridLayoutManager(getContext(), 5));
        portraitRV.setAdapter(adapter);
    }


    /**
     * 取消选择
     */
    public void cancelPortrait() {
        if (adapter != null) {
            adapter.cancelSelected();
        }
    }


}
